﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using PandaMarket.Models;

namespace PandaMarket.BUS
{
    public class AccountBUS
    {
        public MembershipCreateStatus CreateUserBUS(RegisterModel model)
        {
            // Create in database

            AccountDBDataContext db = new AccountDBDataContext();

            db.sp_InsertAccountsAndDetails(model.UserName, model.Password, 3, model.Avatar, false, 0, 0, model.HoTen, model.NgaySinh, model.NoiSinh, model.DiaChi, model.SoDienThoai, model.MaTheTinDung, model.GioiTinh, model.Email);
            db.SubmitChanges();
            
                       
            return MembershipCreateStatus.Success;
        }
        public bool Validate(LogOnModel model)
        {
            AccountDBDataContext db = new AccountDBDataContext();
            return db.sp_SelectAccountByNameAndPassword(model.UserName, model.Password).Count()>0;
        }

        public Models.ProfileModel GetProfile(string username)
        {
            Models.ProfileModel acc = new ProfileModel();
            AccountDBDataContext db = new AccountDBDataContext();
            var taikhoan = db.TAI_KHOANs.First(c => c.TenTaiKhoan == username);
            var thongtintaikhoan = db.THONG_TIN_TKs.First(t => t.MaTaiKhoan == taikhoan.MaTaiKhoan);

            acc.Avatar = taikhoan.Avatar;
            acc.BiKhoa = taikhoan.BiKhoa;
            acc.Password = taikhoan.MatKhau;
            acc.DiaChi = thongtintaikhoan.DiaChi;
            acc.DiemTinCayBan = taikhoan.DiemTinCayBan;
            acc.DiemTinCayMua = taikhoan.DiemTinCayMua;
            acc.Email = thongtintaikhoan.EMail;
            acc.GioiTinh = thongtintaikhoan.GioiTinh;
            acc.HoTen = thongtintaikhoan.HoTen;
            acc.MaTaiKhoan = taikhoan.MaTaiKhoan;
            acc.MaTheTinDung = thongtintaikhoan.MaTheTinDung;
            acc.MaThongTinTK = thongtintaikhoan.MaThongTinTK;
            acc.NgaySinh = thongtintaikhoan.NgaySinh;
            acc.NoiSinh = thongtintaikhoan.NoiSinh;
            acc.SoDienThoai = thongtintaikhoan.SoDienThoai;
            acc.UserName = taikhoan.TenTaiKhoan;
            
            return acc;
        }
        public MembershipCreateStatus UpdateUserBUS(ProfileModel model)
        {
            // Create in database

            AccountDBDataContext db = new AccountDBDataContext();

            int valueresult = db.sp_UpdateAccountDetail(model.MaTaiKhoan, model.HoTen, model.NgaySinh, model.NoiSinh, model.DiaChi, model.SoDienThoai, model.MaTheTinDung, model.GioiTinh, model.Email);
            int valueresult2 = db.sp_UpdateAccount(model.MaTaiKhoan, model.UserName,model.Password,3,model.Avatar,model.BiKhoa,model.DiemTinCayBan,model.DiemTinCayMua);

            db.SubmitChanges();
            
            //if (valueresult)
            return MembershipCreateStatus.Success;
        }
        public MembershipCreateStatus UpdateUserPasswordBUS(int maTaiKhoan, ChangePasswordModel model)
        {
            // Create in database

            AccountDBDataContext db = new AccountDBDataContext();
            if (db.TAI_KHOANs.Count(c => c.MaTaiKhoan == maTaiKhoan && c.MatKhau == model.OldPassword) > 0)
            {
                int valueresult = db.sp_UpdateAccountPassword(maTaiKhoan, model.NewPassword);
                db.SubmitChanges();
                return MembershipCreateStatus.Success;
            }
            else
            {
                return MembershipCreateStatus.ProviderError;
            }
            //if (valueresult)
            
        }
    }
}
